package com.seu.clab.farm.service.impl;

import com.seu.clab.farm.exception.BizException;
import com.seu.clab.farm.model.dao.entity.Payment;
import com.seu.clab.farm.model.dao.mapper.AdminMapper;
import com.seu.clab.farm.model.dao.mapper.PaymentMapper;
import com.seu.clab.farm.service.PaymentService;
import com.seu.clab.farm.util.ConstantUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PaymentServiceImpl implements PaymentService {
    @Autowired
    private PaymentMapper paymentMapper;

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 设置付款码(管理员权限)
     * @param url 需要设置的url
     * @param accountId
     */
    @Override
    public void setPayPic(String url,String accountId) {
        //检查权限
//        Admin admin = adminMapper.selectByPrimaryKey(accountId);
//        if(admin==null)
//            throw new BizException(ConstantUtil.BizExceptionCause.LOW_AUTHORITY);
        //把之前的付款二维码全部删掉
        List<Payment> list=paymentMapper.selectAll();
        for (int i = 0; i < list.size(); i++) {
            paymentMapper.deleteByPrimaryKey(list.get(i).getId());
        }
        Payment payment=new Payment();
        payment.setUrl(url);
        paymentMapper.insert(payment);
    }

    /**
     *请求付款码url
     * @return
     */
    @Override
    public String showPayPic() {
        List<Payment> list=paymentMapper.selectAll();
        //检查是否有唯一的付款码
        if(list.size()!=1)
            throw new BizException(ConstantUtil.BizExceptionCause.PAYMENT_PICTURE_ERROR);
        return list.get(0).getUrl();
    }
}
